package com.ruoyi.doorcontrol.quartz;

import com.ruoyi.common.core.domain.entity.SysDictData;

import com.ruoyi.doorcontrol.domain.ThDoorControl;
import com.ruoyi.doorcontrol.domain.ThDoorcontrolUser;
import com.ruoyi.doorcontrol.service.HkUserService;
import com.ruoyi.doorcontrol.service.IThDoorControlService;
import com.ruoyi.doorcontrol.service.IThDoorcontrolUserService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
@EnableScheduling //开启定时任务
public class HkQuartz {

    @Autowired
    private IThDoorcontrolUserService thDoorcontrolUserService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private IThDoorControlService thDoorControlService;
    @Autowired
    private HkUserService hkUserService;

    @Scheduled(cron = "0 0 4 * * ?") //定时任务注解+cron表达式
    //@Scheduled(cron = "0 35 15 ? * *  ") //定时任务注解+cron表达式
    @Transactional
    public void delControlUser(){
        System.out.println("定时任务执行一次"+new Date());
        ThDoorcontrolUser thDoorcontrolUser = new ThDoorcontrolUser();
        thDoorcontrolUser.setStatus("0");
        List<ThDoorcontrolUser> thDoorcontrolUsers = thDoorcontrolUserService.selectThDoorcontrolUserList(thDoorcontrolUser);
        SysDictData sysDictData = sysDictDataService.selectDictValue("zy_visit","visit_effective_deluser");

        Date date = new Date();
        for (ThDoorcontrolUser doorcontrolUser : thDoorcontrolUsers) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(thDoorcontrolUser.getEndTime());
            instance.add(Calendar.HOUR,Integer.parseInt(sysDictData.getDictValue()));

            //if(date.compareTo(doorcontrolUser.getEndTime())>Integer.parseInt(sysDictData.getDictValue())){
            if(date.compareTo(instance.getTime())<1){
                doorcontrolUser.setStatus("1");
                ThDoorControl thDoorControl_in = thDoorControlService.selectcontrolByPositionAndType("menfang","in");
                hkUserService.deleteUserInfo(thDoorControl_in,doorcontrolUser.getEmployeeNo());
                ThDoorControl thDoorControl_out = thDoorControlService.selectcontrolByPositionAndType("menfang","out");
                hkUserService.deleteUserInfo(thDoorControl_out,doorcontrolUser.getEmployeeNo());
                thDoorcontrolUserService.updateThDoorcontrolUser(doorcontrolUser);
            }
        }
    }



}
